package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.ProductItemQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.ProductItem;
import cn.org.eshow.service.ProductItemManager;
import cn.org.eshow.service.ProductManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"list", "search",  "delete", "view", "update", "save"})
public class ProductItemAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductManager productManager;
    @Autowired
    private ProductItemManager productItemManager;

    private List<ProductItem> productItems=new ArrayList<ProductItem>();
    private ProductItem productItem=new ProductItem();
    private ProductItemQuery query = new ProductItemQuery();

    /**
     * @return
     */
    public String list() {
        productItems = productItemManager.list(query);
        return LIST;
    }

    /**
     * @return
     */
    public String search() {
        Page<ProductItem> page = productItemManager.search(query);
        productItems = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }


    /**
     *
     */
    public void delete() {
        productItem = productItemManager.get(id);
        if (productItem != null) {
            productItem.setEnabled(Boolean.FALSE);
            productItemManager.save(productItem);
            RenderUtil.success("删除成功");
            saveMessage("删除成功");
        } else {
            RenderUtil.failure("参数不正确");
            saveMessage("参数不正确");
        }
    }

    /**
     * @return
     */
    public String view() {
        if (id != null) {
            productItem = productItemManager.get(id);
        }
        return NONE;
    }

    /**
     * @return
     * @throws Exception
     */
    public String update() throws Exception {
        ProductItem old = productItemManager.get(id);
        old = productItemManager.updateProductItem(old,productItem);
        saveMessage("修改成功");
        return REDIRECT;
    }

    /**
     * @return
     * @throws Exception
     */
    public String save() throws Exception {
        productItem.setProduct(productManager.get(productItem.getProduct().getId()));
        productItem = productItemManager.save(productItem);
        saveMessage("添加成功");
        return REDIRECT;
    }

    public List<ProductItem> getProductItems() {
        return productItems;
    }

    public void setProductItems(List<ProductItem> productItems) {
        this.productItems = productItems;
    }

    public ProductItem getProductItem() {
        return productItem;
    }

    public void setProductItem(ProductItem productItem) {
        this.productItem = productItem;
    }

    public ProductItemQuery getQuery() {
        return query;
    }

    public void setQuery(ProductItemQuery query) {
        this.query = query;
    }


}